package com.hjm.ui.order;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.order.adapter.OrderItemAdapter;
import com.hjm.ui.order.entity.Order;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单项
 */
public class OrderItem extends LinearLayout{
    private List<? extends Order> orderList;
    private RecyclerView recyclerView;
    private OrderItemAdapter orderItemAdapter;
    private OrderItemAdapter.OnItemClickListener onItemClickListener;

    public OrderItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 在这里添加你的布局和逻辑
        LayoutInflater.from(getContext()).inflate(R.layout.layout_order_item, this, true);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1)); // 设置为一列的网格布局
        if (orderList == null){
            orderList = new ArrayList<>();
        } else {
            Log.d("订单项", "init: 设置到适配器 "+orderList.size());
            orderItemAdapter.setOrderList(orderList);
        }
        orderItemAdapter = new OrderItemAdapter(orderList);
        if (onItemClickListener != null){
            orderItemAdapter.setOnItemClickListener(onItemClickListener);
        }
        recyclerView.setAdapter(orderItemAdapter);
    }

    public void setOrderList(List<? extends Order> orderList) {
       if (orderItemAdapter != null){
           Log.d("订单项", "setOrderList: 设置到适配器 "+orderList.size());
           this.orderList = orderList;
           orderItemAdapter.setOrderList(orderList);
       }
    }

    public void setOnItemClickListener(OrderItemAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
        if (orderItemAdapter != null){
            orderItemAdapter.setOnItemClickListener(listener);
        }
    }
}
